<template>
  <view class="feedback-container">
    <!-- 常见问题 -->
    <view class="faq-section">
      <view class="section-title">常见问题</view>
      <view class="faq-list">
        <view 
          class="faq-item"
          v-for="(item, index) in faqList"
          :key="index"
          @click="toggleFaq(index)"
        >
          <view class="question">
            <text>{{ item.question }}</text>
            <text class="arrow" :class="{ 'open': item.isOpen }">></text>
          </view>
          <view class="answer" v-if="item.isOpen">{{ item.answer }}</view>
        </view>
      </view>
    </view>

    <!-- 问题反馈 -->
    <view class="feedback-section">
      <view class="section-title">问题反馈</view>
      <textarea
        v-model="feedbackContent"
        class="feedback-input"
        placeholder="请描述您遇到的问题或建议（最少10个字）"
        :maxlength="500"
      ></textarea>
      <view class="contact-input">
        <input
          v-model="contactInfo"
          type="text"
          placeholder="请留下您的联系方式（选填）"
        />
      </view>
      <button class="submit-btn" @click="submitFeedback">提交反馈</button>
    </view>
  </view>
</template>

<script>
import { createFeedback } from '@/api/feedback'

export default {
  data() {
    return {
      faqList: [
        {
          question: '如何预约挂号？',
          answer: '进入首页选择医院和科室，选择医生后点击预约按钮，填写相关信息即可完成预约。',
          isOpen: false
        },
        {
          question: '如何取消预约？',
          answer: '在"我的预约"中找到相应预约记录，点击取消预约按钮即可。请注意取消预约的时间限制。',
          isOpen: false
        },
        {
          question: '预约成功后如何就医？',
          answer: '预约成功后，请在就诊当天携带有效证件，提前30分钟到达医院，凭预约信息到相应科室就诊。',
          isOpen: false
        }
      ],
      feedbackContent: '',
      contactInfo: ''
    }
  },

  methods: {
    toggleFaq(index) {
      this.faqList[index].isOpen = !this.faqList[index].isOpen
    },

    async submitFeedback() {
      if (this.feedbackContent.length < 10) {
        uni.showToast({
          title: '反馈内容至少10个字',
          icon: 'none'
        })
        return
      }

      // 如果填写了手机号，则验证格式
      if (this.contactInfo && !/^1[3-9]\d{9}$/.test(this.contactInfo)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
        return
      }

      try {
        uni.showLoading({
          title: '提交中...'
        })

        // 直接从本地获取userId
        const userId = uni.getStorageSync('userId')
        console.log('userId:', userId)
        
        // 调用反馈接口
        await createFeedback({
          userId: userId,
          content: this.feedbackContent,
          phone: this.contactInfo || undefined
        })

        uni.hideLoading()
        uni.showToast({
          title: '反馈提交成功',
          icon: 'success'
        })
        
        // 清空输入
        this.feedbackContent = ''
        this.contactInfo = ''
        
        // 返回上一页
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
        
      } catch (error) {
        uni.hideLoading()
        uni.showToast({
          title: error.message || '提交失败',
          icon: 'none'
        })
      }
    }
  }
}
</script>

<style lang="scss">
.feedback-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;

  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin: 30rpx 0 20rpx;
    padding-left: 20rpx;
    border-left: 8rpx solid #007AFF;
  }

  .faq-section {
    background: #fff;
    border-radius: 20rpx;
    padding: 20rpx;
    margin-bottom: 30rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);

    .faq-item {
      margin-bottom: 20rpx;

      .question {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20rpx;
        background: #f9f9f9;
        border-radius: 10rpx;
        font-size: 28rpx;
        color: #333;

        .arrow {
          color: #999;
          transform: rotate(90deg);
          transition: transform 0.3s;

          &.open {
            transform: rotate(-90deg);
          }
        }
      }

      .answer {
        padding: 20rpx;
        font-size: 26rpx;
        color: #666;
        line-height: 1.6;
        background: #fff;
        border-radius: 10rpx;
        margin-top: 10rpx;
      }
    }
  }

  .feedback-section {
    background: #fff;
    border-radius: 20rpx;
    padding: 20rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);

    .feedback-input {
      width: 100%;
      height: 300rpx;
      background: #f9f9f9;
      border-radius: 10rpx;
      padding: 20rpx;
      font-size: 28rpx;
      margin-bottom: 20rpx;
    }

    .contact-input {
      margin-bottom: 30rpx;

      input {
        width: 100%;
        height: 80rpx;
        background: #f9f9f9;
        border-radius: 10rpx;
        padding: 0 20rpx;
        font-size: 28rpx;
      }
    }

    .submit-btn {
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      background: #007AFF;
      color: #fff;
      border-radius: 40rpx;
      font-size: 30rpx;
      margin-top: 30rpx;

      &:active {
        opacity: 0.8;
      }
    }
  }
}
</style> 